﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ES_Project.Model;
using System.Data;
using System.Data.SqlClient;

using System.Collections;
using System.Web.Security;

using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;

using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace ES_Project.Pages
{
    public partial class WebForm27 : System.Web.UI.Page
    {
        string StrConn = WebConfigurationManager.ConnectionStrings["baseessystem"].ToString();
        MySqlCommand objCmd = new MySqlCommand(); 
        protected void Page_Load(object sender, EventArgs e)
        {
            Button1.Enabled = false;
            st_id.Text = (string)Session["st_id"];
            TextBox35.Text = (string)Session["st_id"];
            TextBox40.Visible = false;
            TextBox29.Visible = true;
            if (!Page.IsPostBack)
            {
                //TextBox47.Text = DateTime.Now.ToString("dd/MM/yyyy");
                //TextBox21.Text = DateTime.Now.ToString("dd/MM/yyyy");
                Binddata();
                Bindstid();
                discountdata();
            }
        }
        private void clear()
        {
            TextBox2.Text = "";
            TextBox4.Text = "";
            TextArea2.Value = "";

            TextBox3.Text = "";
            TextBox11.Text = "";
            TextBox6.Text = "";
            TextBox8.Text = "";
            TextBox10.Text = "";
            TextBox7.Text = "";


            TextBox12.Text = "";
            TextArea1.Value = "";


        }
        void cleargrid2()
        {

            TextBox22.Text = "";
            TextBox23.Text = "";
            GridView3.DataSource = null;
            GridView3.DataBind();
            GridView4.DataSource = null;
            GridView4.DataBind();

        }
        void Binddata()
        {
            MySqlConnection Conn = new MySqlConnection(StrConn);

            
           

           
            //data student_mast
            String strSQL;
            strSQL = "SELECT * FROM student_mast  where st_id = '" + this.st_id.Text + "' ";
            //strSQL = "SELECT * FROM student_detail  inner join cls_register_detail   on student_detail.cls_reg_detail_id = cls_register_detail.cls_reg_detail_id inner join cls_register_mast on cls_register_mast.cls_reg_id = cls_register_detail.cls_reg_id  where s WHERE   tbDCusCardS.CardSNo  ='" + this.Edit_st_id.Text + "' ";
            MySqlDataAdapter da = new MySqlDataAdapter(strSQL, Conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "student_mast");
            GridView1.DataSource = ds.Tables["student_mast"];
            GridView1.DataBind();
            da.Fill(ds);
            int _row = ds.Tables[0].Rows.Count;
            int i = 0;
            if (_row > i)
            {


                TextBox2.Text = (string)ds.Tables[0].Rows[i]["st_code"].ToString();
                TextBox3.Text = (string)ds.Tables[0].Rows[i]["st_name"].ToString();
                TextBox4.Text = (string)ds.Tables[0].Rows[i]["st_nickname"].ToString();
                //TextBox5.Text = (string)ds.Tables[0].Rows[i]["st_name"].ToString();
                RadioButtonList1.SelectedValue = (string)ds.Tables[0].Rows[i]["st_gender"].ToString();
                TextBox11.Text = (string)ds.Tables[0].Rows[i]["st_phone"].ToString();
                TextBox6.Text = (string)ds.Tables[0].Rows[i]["st_school"].ToString();
                TextBox8.Text = (string)ds.Tables[0].Rows[i]["st_birthday"].ToString();
                TextBox10.Text = (string)ds.Tables[0].Rows[i]["st_age"].ToString();
                TextBox7.Text = (string)ds.Tables[0].Rows[i]["st_email"].ToString();
                TextBox21.Text = (string)ds.Tables[0].Rows[i]["register_date"].ToString();
                TextBox12.Text = (string)ds.Tables[0].Rows[i]["st_end_date"].ToString();              
                TextArea1.Value = (string)ds.Tables[0].Rows[i]["objective"].ToString();
                TextBox29.Text = (string)ds.Tables[0].Rows[i]["family_code"].ToString();
                TextBox33.Text = (string)ds.Tables[0].Rows[i]["family_name"].ToString();
                TextBox14.Text = (string)ds.Tables[0].Rows[i]["mother_name"].ToString();
                TextBox30.Text = (string)ds.Tables[0].Rows[i]["parent_name"].ToString();
                TextArea2.Value= (string)ds.Tables[0].Rows[i]["address"].ToString();
                TextBox19.Text = (string)ds.Tables[0].Rows[i]["email_father"].ToString();
                TextBox15.Text= (string)ds.Tables[0].Rows[i]["phone_father"].ToString();
                TextBox20.Text = (string)ds.Tables[0].Rows[i]["email_mother"].ToString();
                TextBox16.Text= (string)ds.Tables[0].Rows[i]["phone_mother"].ToString();
                TextBox32.Text= (string)ds.Tables[0].Rows[i]["email_parent"].ToString();
              TextBox31.Text= (string)ds.Tables[0].Rows[i]["phone_parent"].ToString();
                TextBox13.Text = (string)ds.Tables[0].Rows[i]["father_name"].ToString();
                familycode.Text = (string)ds.Tables[0].Rows[i]["family_code"].ToString();
                DropDownList1.SelectedValue = (string)ds.Tables[0].Rows[i]["id_Discount"].ToString();
                Label39.Text = (string)ds.Tables[0].Rows[i]["id_Discount"].ToString();
                String strSQL2;
                strSQL2 = "SELECT * FROM discount  where id_Discount = '" + this.Label39.Text + "' ";
               
                MySqlDataAdapter da2 = new MySqlDataAdapter(strSQL, Conn);
                DataSet ds2 = new DataSet();
                da2.Fill(ds2, "discount");
                da2.Fill(ds2);
            int _row2 = ds2.Tables[0].Rows.Count;
            int i2 = 0;
            if (_row2 > i2)
            {
                Label40.Text = (string)ds.Tables[0].Rows[i]["Num_Discount"].ToString();
            }
            }
           

        }
        
        protected void Button5_Click(object sender, EventArgs e)
        {

        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            clear();
            TextBox29.Text = "";
            TextBox33.Text = "";
            TextBox30.Text = "";
            TextBox31.Text = "";
            TextBox32.Text = "";
            TextBox13.Text = "";
            TextBox15.Text = "";
            TextBox19.Text = "";
            TextBox14.Text = "";
            TextBox16.Text = "";
            TextBox20.Text = "";

            GridView1.DataSource = null;
            GridView1.DataBind();
            TextBox35.Text = "";
            familycode.Text = "";
            afterfamilycode.Text = "";
            TextBox22.Text = "";
            TextBox23.Text = "";
            GridView3.DataSource = null;
            GridView3.DataBind();
            GridView4.DataSource = null;
            GridView4.DataBind();

        }
        void Searchstidgridview1()
        {
            if (familycode.Text == "")
            {
                MySqlConnection Conn = new MySqlConnection(StrConn);
                String strSQL;
                strSQL = "SELECT * FROM Student_Mast  where Student_Mast.st_id = '" + TextBox35.Text + "' ";
                MySqlDataAdapter da = new MySqlDataAdapter(strSQL, Conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "Student_Mast");
                GridView1.DataSource = ds.Tables["Student_Mast"];
                GridView1.DataBind();
            }
            else
            {
                MySqlConnection Conn = new MySqlConnection(StrConn);
                String strSQL;
                strSQL = "SELECT * FROM Student_Mast left join family_mast on family_mast.family_code = student_mast.family_code where Student_Mast.family_code = '" + familycode.Text + "' or Student_Mast.family_code = '" + afterfamilycode.Text + "' and Student_Mast.register_date = '" + Convert.ToDateTime(TextBox42.Text).AddYears(-543).ToString("yyyy-MM-dd") + "' ";
                MySqlDataAdapter da = new MySqlDataAdapter(strSQL, Conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "Student_Mast");
                GridView1.DataSource = ds.Tables["Student_Mast"];
                GridView1.DataBind();
            }
        }
        void Searchstid()
        {
            MySqlConnection Conn = new MySqlConnection(StrConn);
            String strSQL;
            strSQL = "SELECT st_id FROM Student_Mast where st_code = '" + TextBox34.Text + "' ";
            MySqlDataAdapter da = new MySqlDataAdapter(strSQL, Conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "Student_Mast");

            int _row = ds.Tables[0].Rows.Count;
            int i = 0;
            if (_row > i)
            {


                TextBox35.Text = (string)ds.Tables[0].Rows[i]["st_id"].ToString();


            }



        }
        void Datastudent()
        {
            MySqlConnection Conn3 = new MySqlConnection(StrConn);
            string strSQL3;
            strSQL3 = "SELECT * FROM student_mast WHERE st_code ='" + TextBox34.Text + "'";

            Conn3.Open();
            MySqlDataAdapter da5 = new MySqlDataAdapter(strSQL3, Conn3);
            DataSet ds5 = new DataSet();
            da5.Fill(ds5);

            int _row5 = ds5.Tables[0].Rows.Count;
            int i5 = 0;
            if (_row5 > i5)
            {


                TextBox35.Text = (string)ds5.Tables[0].Rows[i5]["st_id"].ToString();
                //sendpage familyData.aspx
                st_codesend.Text = (string)ds5.Tables[0].Rows[i5]["st_code"].ToString();
                regissend.Text = Convert.ToString(ds5.Tables[0].Rows[i5]["register_date"]);
                facodesend.Text = Convert.ToString(ds5.Tables[0].Rows[i5]["family_code"]);
                fanamesend.Text = (string)ds5.Tables[0].Rows[i5]["family_name"].ToString();


            }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            using (essystemEntities context = new essystemEntities())
            {
                Button button = (Button)sender;
                switch (button.ID)
                {
                    case "Button1":
                        List<student_mast> sm = context.student_mast.Where("it.[st_code]='" + TextBox2.Text + "' ").ToList();
                        if (sm.Count == 0)
                        {
                            student_mast stumast = new student_mast();

                            // stumast.family_id = Convert.ToInt32(TextBox29.Text);
                            stumast.st_code = TextBox2.Text;
                            stumast.st_name = TextBox3.Text;
                            stumast.st_nickname = TextBox4.Text;
                            stumast.st_gender = RadioButtonList1.SelectedValue;
                            stumast.st_phone = TextBox11.Text;
                            stumast.st_school = TextBox6.Text;
                            stumast.st_age = TextBox10.Text;
                            stumast.st_email = TextBox7.Text;
                            stumast.register_date = Convert.ToDateTime(TextBox21.Text);

                            if (!TextBox12.Text.Trim().Equals(""))
                            {
                                stumast.st_end_date = Convert.ToDateTime(TextBox12.Text);
                            }
                            //stumast.remark = 
                            stumast.remark = TextArea1.Value;
                            if (!TextBox8.Text.Trim().Equals(""))
                            {
                                stumast.st_birthday = Convert.ToDateTime(TextBox8.Text);
                            }
                            if (!TextBox29.Text.Trim().Equals(""))
                            {
                                stumast.family_code = TextBox29.Text;
                            }
                            stumast.family_name = TextBox33.Text;
                            stumast.father_name = TextBox13.Text;
                            stumast.mother_name = TextBox14.Text;
                            stumast.parent_name = TextBox30.Text;

                            stumast.address = TextArea2.Value;
                            stumast.email_father = TextBox19.Text;
                            stumast.phone_father = TextBox15.Text;
                            stumast.email_mother = TextBox20.Text;
                            stumast.phone_mother = TextBox16.Text;
                            stumast.email_parent = TextBox32.Text;
                            stumast.phone_parent = TextBox31.Text;
                            stumast.id_Discount = DropDownList1.SelectedValue.ToString();


                            context.student_mast.AddObject(stumast);
                            //table familymast
                            //if (TextBox29.Text != "")
                            //{
                            //    List<family_mast> fm = context.family_mast.Where("it.[family_code]='" + TextBox29.Text + "' ").ToList();
                            //    if (fm.Count == 0)
                            //    {
                            //        family_mast familymast = new family_mast();

                            //        familymast.family_code = TextBox29.Text;
                            //        familymast.father_name = TextBox13.Text;
                            //        familymast.family_name = TextBox33.Text;
                            //        familymast.mother_name = TextBox14.Text;
                            //        familymast.parent_name = TextBox30.Text;

                            //        familymast.address = TextArea2.Value;
                            //        familymast.email_father = TextBox32.Text;
                            //        familymast.phone_father = TextBox15.Text;
                            //        familymast.email_mother = TextBox20.Text;
                            //        familymast.phone_mother = TextBox16.Text;
                            //        familymast.email_parent = TextBox32.Text;
                            //        familymast.phone_parent = TextBox31.Text;
                            //        context.family_mast.AddObject(familymast);
                            //    }
                            //}
                            if (context.SaveChanges() > 0)
                            {

                                ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Alert1", "alert('บันทึกเรียบร้อยแล้ว')", true);
                                GridView1.DataBind();
                                GridView1.SelectedIndex = -1;
                                clear();
                            }
                            TextBox34.Text = stumast.st_code;
                            familycode.Text = stumast.family_code;
                            TextBox42.Text = string.Format("{0:dd/MM/yyyy}", stumast.register_date).ToString();
                        }

                        else
                        {
                            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "ReturnMessini9", "alert('รหัสนักเรียนซ้ำ');", true);

                        }

                        break;
                    case "Button5":

                        List<student_mast> smast = context.student_mast.Where("it.[st_id]=" + st_id.Text + " ").ToList();
                        if (smast.Count() == 1)
                        {
                            student_mast smastEdit = context.student_mast.Where("it.[st_id]=" + st_id.Text + "").First();
                            if (smastEdit != null)
                            {
                                smastEdit.st_code = TextBox2.Text;
                                smastEdit.st_name = TextBox3.Text;
                                smastEdit.st_nickname = TextBox4.Text;
                                smastEdit.st_gender = RadioButtonList1.SelectedValue;
                                smastEdit.st_phone = TextBox11.Text;

                                smastEdit.st_age = TextBox10.Text;
                                smastEdit.st_email = TextBox7.Text;
                                smastEdit.register_date = Convert.ToDateTime(TextBox21.Text);

                                smastEdit.st_school = TextBox6.Text;
                                smastEdit.remark = TextArea1.Value;
                                //smastEdit.family_id = Convert.ToInt32(TextBox29.Text);
                                if (!TextBox8.Text.Trim().Equals(""))
                                {
                                    smastEdit.st_birthday = Convert.ToDateTime(TextBox8.Text);
                                }
                                if (!TextBox12.Text.Trim().Equals(""))
                                {
                                    smastEdit.st_end_date = Convert.ToDateTime(TextBox12.Text);
                                }
                                if (!TextBox40.Text.Trim().Equals(""))
                                {

                                    smastEdit.family_code = TextBox40.Text;
                                }
                                smastEdit.family_name = TextBox33.Text;
                                smastEdit.mother_name = TextBox14.Text;
                                smastEdit.parent_name = TextBox30.Text;

                                smastEdit.address = TextArea2.Value;
                                smastEdit.email_father = TextBox19.Text;
                                smastEdit.phone_father = TextBox15.Text;
                                smastEdit.email_mother = TextBox20.Text;
                                smastEdit.phone_mother = TextBox16.Text;
                                smastEdit.email_parent = TextBox32.Text;
                                smastEdit.phone_parent = TextBox31.Text;
                                smastEdit.father_name = TextBox13.Text;
                                afterfamilycode.Text = smastEdit.family_code;

                                //afteredit textbox
                                TextBox29.Text = smastEdit.family_code;
                                TextBox33.Text = smastEdit.family_name;
                                TextBox30.Text = smastEdit.parent_name;
                                TextBox31.Text = smastEdit.phone_parent;
                                TextBox32.Text = smastEdit.email_parent;
                                TextBox13.Text = smastEdit.father_name;
                                TextBox15.Text = smastEdit.phone_father;
                                TextBox19.Text = smastEdit.email_father;
                                TextBox14.Text = smastEdit.mother_name;
                                TextBox16.Text = smastEdit.phone_mother;
                                TextBox20.Text = smastEdit.email_mother;
                                TextArea2.Value = smastEdit.address;
                                DropDownList1.SelectedValue = smastEdit.id_Discount;
                                //  edit table familymast


                                //List<family_mast> fmast = context.family_mast.Where("it.[family_id]=" + idfamily.Text + " ").ToList();
                                //if (fmast.Count() == 1)
                                //{
                                //    family_mast familyEdit = context.family_mast.Where("it.[family_id]=" + idfamily.Text + "").First();
                                //    if (familyEdit != null)
                                //    {





                                //        familyEdit.family_code = TextBox40.Text;
                                //        familyEdit.father_name = TextBox13.Text;
                                //        familyEdit.family_name = TextBox33.Text;
                                //        familyEdit.mother_name = TextBox14.Text;
                                //        familyEdit.parent_name = TextBox30.Text;

                                //        familyEdit.address = TextArea2.Value;
                                //        familyEdit.email_father = TextBox32.Text;
                                //        familyEdit.phone_father = TextBox15.Text;
                                //        familyEdit.email_mother = TextBox20.Text;
                                //        familyEdit.phone_mother = TextBox16.Text;
                                //        familyEdit.email_parent = TextBox32.Text;
                                //        familyEdit.phone_parent = TextBox31.Text;

                                //    }
                                //}


                                if (context.SaveChanges() > 0)
                                {

                                    ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Alert2", "alert('แก้ไขข้อมูลเรียบร้อยแล้ว')", true);
                                    GridView1.DataBind();
                                    GridView1.SelectedIndex = -1;
                                    Button1.Enabled = true;
                                    Button5.Enabled = false;
                                    clear();
                                    TextBox33.Text = "";
                                    TextBox13.Text = "";
                                    TextBox15.Text = "";
                                    TextBox19.Text = "";
                                    TextBox14.Text = "";
                                    TextBox16.Text = "";
                                    TextBox20.Text = "";
                                    TextArea2.Value = "";
                                    TextBox30.Text = "";
                                    TextBox31.Text = "";
                                    TextBox32.Text = "";
                                    TextBox29.Text = "";
                                }
                            }

                        }

                        break;

                }

            }
            TextBox21.Text = DateTime.Now.ToString("dd/MM/yyyy");
            Datastudent();
            Searchstidgridview1();
            TextBox22.Text = "";
            TextBox23.Text = "";
            GridView3.DataSource = null;
            GridView3.DataBind();

        }
        void wherefamily()
        {
            using (essystemEntities context = new essystemEntities())
            {
                List<student_mast> chcfamily = context.student_mast.Where("it.[family_code]='" + TextBox29.Text + "'").ToList();
                if (chcfamily.Count() >= 1)
                {
                    student_mast family = chcfamily[0];
                    //Branchid.Text = Branch.BranchID.ToString();
                    TextBox33.Text = family.family_name;
                    TextBox13.Text = family.father_name;
                    TextBox15.Text = family.phone_father;
                    TextBox19.Text = family.email_father;
                    TextBox14.Text = family.mother_name;
                    TextBox16.Text = family.phone_mother;
                    TextBox20.Text = family.email_mother;
                    TextArea2.Value = family.address;

                    TextBox30.Text = family.parent_name;
                    TextBox31.Text = family.phone_parent;
                    TextBox32.Text = family.email_parent;

                }
                else
                {
                    TextBox33.Text = "";
                    TextBox13.Text = "";
                    TextBox15.Text = "";
                    TextBox19.Text = "";
                    TextBox14.Text = "";
                    TextBox16.Text = "";
                    TextBox20.Text = "";
                    TextArea2.Value = "";
                    TextBox30.Text = "";
                    TextBox31.Text = "";
                    TextBox32.Text = "";


                }
                ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "Returnhidecode", "YAHOO.example.container.wait.hide();", true);
            }
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            wherefamily();
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Button1.Enabled = false;
            Button5.Enabled = true;
            TextBox40.Visible = true;
            TextBox29.Visible = false;
            using (essystemEntities context = new essystemEntities())
            {

                List<student_mast> sma = context.student_mast.Where("it.[st_id]=" + ((HiddenField)GridView1.SelectedRow.Cells[1].FindControl("HiddenField1")).Value.ToString() + "").ToList();
                if (sma.Count > 0)
                {
                    List<student_mast> sm = context.student_mast.Where("it.[st_id]=" + ((HiddenField)GridView1.SelectedRow.Cells[1].FindControl("HiddenField1")).Value.ToString() + "").ToList();
                    if (sm.Count > 0)
                    {




                        TextBox2.Text = sm[0].st_code;
                        TextBox3.Text = sm[0].st_name;

                        //TextBox29.Text = sm[0].family_id.ToString();
                        TextBox4.Text = sm[0].st_nickname;
                        TextBox11.Text = sm[0].st_phone;
                        TextBox6.Text = sm[0].st_school;
                        TextBox10.Text = sm[0].st_age;
                        TextBox7.Text = sm[0].st_email;

                        TextBox21.Text = string.Format("{0:dd/MM/yyyy}", sm[0].register_date).ToString();
                        TextBox12.Text = string.Format("{0:dd/MM/yyyy}", sm[0].st_end_date).ToString();
                        RadioButtonList1.SelectedValue = sm[0].st_gender;
                        TextBox8.Text = string.Format("{0:dd/MM/yyyy}", sm[0].st_birthday).ToString();
                        TextArea1.Value = sm[0].objective;
                        st_id.Text = Convert.ToString(sm[0].st_id);
                        //if (!TextBox29.Text.Trim().Equals(""))
                        //{
                        TextBox40.Text = sm[0].family_code;
                        //}
                        TextBox33.Text = sm[0].family_name;

                        TextBox14.Text = sm[0].mother_name;

                        TextBox30.Text = sm[0].parent_name;
                        TextArea2.Value = sm[0].address;

                        TextBox19.Text = sm[0].email_father;
                        TextBox15.Text = sm[0].phone_father;
                        TextBox20.Text = sm[0].email_mother;
                        TextBox16.Text = sm[0].phone_mother;
                        TextBox32.Text = sm[0].email_parent;
                        TextBox31.Text = sm[0].phone_parent;
                        TextBox13.Text = sm[0].father_name;
                        DropDownList1.SelectedValue = sm[0].id_Discount;
                        //List<family_mast> fma = context.family_mast.Where("it.[family_id]=" + ((Label)GridView1.SelectedRow.Cells[1].FindControl("Label18")).Text.ToString() + "").ToList();
                        //if (sma.Count > 0)
                        //{
                        //    List<family_mast> fm = context.family_mast.Where("it.[family_id]=" + ((Label)GridView1.SelectedRow.Cells[1].FindControl("Label18")).Text.ToString() + "").ToList();
                        //    if (fm.Count > 0)
                        //    {
                        //        idfamily.Text = Convert.ToString(fm[0].family_id);
                        //    }
                        //}
                    }
                }


            }

        }
        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // loop all data rows
                foreach (DataControlFieldCell cell in e.Row.Cells)
                {
                    // check all cells in one row
                    foreach (Control control in cell.Controls)
                    {
                        // Must use LinkButton here instead of ImageButton
                        // if you are having Links (not images) as the command button.
                        ImageButton button = control as ImageButton;
                        if (button != null && button.CommandName == "Delete")
                            // Add delete confirmation
                            button.OnClientClick = "if (!confirm('คุณต้องการที่จะลบข้อมูล?')) return;";
                    }
                }
            }
        }


        protected void Button3_Click(object sender, EventArgs e)
        {


            MySqlConnection Conn = new MySqlConnection(StrConn);


            Conn.Open();
            string strSQL = "";
            RadioButton chkDel;
            //CheckBox 
            Label lblID;

            int i;
            for (i = 0; i <= GridView3.Rows.Count - 1; i++)
            {
                chkDel = (RadioButton)GridView3.Rows[i].FindControl("rdobutton");
                lblID = (Label)GridView3.Rows[i].FindControl("Label27");
                if (chkDel.Checked)
                {


                    strSQL = "INSERT INTO student_detail (st_id,emp_cls_mast_id) VALUES ('" + this.TextBox35.Text + "','" + lblID.Text + "')";





                    MySqlCommand objCmd = new MySqlCommand();
                    MySqlDataAdapter da4 = new MySqlDataAdapter(strSQL, Conn);

                    objCmd.Connection = Conn;
                    objCmd.CommandType = CommandType.Text;
                    objCmd.CommandText = strSQL;

                    objCmd.ExecuteNonQuery();
                    Conn.Close();
                    //// คำนวณวัน
                    //string date = TextBox47.Text;

                    //int m = Convert.ToInt32(TextBox45.Text);
                    //for (int num = 1; num <= m; num++)
                    //{

                    //    if (num == 1)
                    //    {

                    //        MySqlConnection Conn2 = new MySqlConnection(StrConn);
                    //        string strSQL2;
                    //        strSQL2 = "INSERT INTO student_schedule (st_dateschedule,emp_cls_mast_id,st_id)   VALUES ('" + Convert.ToDateTime(TextBox47.Text).AddYears(-543).ToString("yyyy-MM-dd") + "','" + TextBox46.Text + "' ,'" + TextBox35.Text + "')";

                    //        MySqlDataAdapter da2 = new MySqlDataAdapter(strSQL2, Conn2);
                    //        Conn2.Open();
                    //        objCmd.Connection = Conn2;
                    //        objCmd.CommandType = CommandType.Text;
                    //        objCmd.CommandText = strSQL2;

                    //        objCmd.ExecuteNonQuery();

                    //    }
                    //    else
                    //    {
                    //        TextBox47.Text = date;

                    //        date = Convert.ToDateTime(TextBox47.Text).AddDays(7).ToString();

                    //        MySqlConnection Conn3 = new MySqlConnection(StrConn);
                    //        string strSQL3;
                    //        strSQL3 = "INSERT INTO student_schedule (st_dateschedule,emp_cls_mast_id,st_id)   VALUES ('" + Convert.ToDateTime(date).AddYears(-543).ToString("yyyy-MM-dd") + "','" + TextBox46.Text + "','" + TextBox35.Text + "' )";

                    //        MySqlDataAdapter da3 = new MySqlDataAdapter(strSQL3, Conn3);
                    //        Conn3.Open();
                    //        objCmd.Connection = Conn3;
                    //        objCmd.CommandType = CommandType.Text;
                    //        objCmd.CommandText = strSQL3;

                    //        objCmd.ExecuteNonQuery();


                    //    }

                    //}
                }

            }



            //TextBox47.Text = DateTime.Now.ToString("dd/MM/yyyy");
            ScriptManager.RegisterClientScriptBlock(this, typeof(Page), UniqueID, "alert('บันทึกข้อมูลเรียบร้อยแล้วค่ะ')", true);
            Bindstid();

            TextBox45.Text = "";
            TextBox46.Text = "";
        }

        void Bindstid()
        {
            if (familycode.Text == "")
            {
                MySqlConnection Conn = new MySqlConnection(StrConn);
                String strSQL;
                //strSQL = "SELECT * FROM student_detail where st_id = '" + TextBox35.Text + "' ";
                //strSQL = "SELECT * FROM student_detail  inner join cls_register_detail   on student_detail.cls_reg_detail_id = cls_register_detail.cls_reg_detail_id inner join cls_register_mast on cls_register_mast.cls_reg_id = cls_register_detail.cls_reg_id inner join student_mast on student_detail.st_id = student_mast.st_id  where student_detail.st_id = '" + TextBox35.Text + "' ";
                strSQL = "select * from student_detail inner join emp_cls_mast on emp_cls_mast.emp_cls_mast_id = student_detail.emp_cls_mast_id inner join student_mast on student_detail.st_id = student_mast.st_id inner join cls_register_detail on cls_register_detail.cls_reg_detail_id = emp_cls_mast.cls_reg_detail_id inner join cls_register_mast on cls_register_mast.cls_reg_id  = cls_register_detail.cls_reg_id inner join emp_mast on emp_cls_mast.emp_id  =  emp_mast.emp_id where student_detail.st_id = '" + TextBox35.Text + "'";
                MySqlDataAdapter da = new MySqlDataAdapter(strSQL, Conn);
                DataSet ds = new DataSet();
                da.Fill(ds, "student_detail");
                GridView4.DataSource = ds.Tables["student_detail"];
                GridView4.DataBind();
            }
            if (familycode.Text != "")
            {
                MySqlConnection Conn2 = new MySqlConnection(StrConn);
                String strSQL2;
                //strSQL = "SELECT * FROM student_detail where st_id = '" + TextBox35.Text + "' ";
                //strSQL = "SELECT * FROM student_detail  inner join cls_register_detail   on student_detail.cls_reg_detail_id = cls_register_detail.cls_reg_detail_id inner join cls_register_mast on cls_register_mast.cls_reg_id = cls_register_detail.cls_reg_id inner join student_mast on student_detail.st_id = student_mast.st_id   where student_mast.family_code = '" + familycode.Text + "' and student_mast.register_date = '" + Convert.ToDateTime(TextBox42.Text).AddYears(-543).ToString("yyyy-MM-dd") + "' ";
                strSQL2 = "select * from student_detail inner join emp_cls_mast on emp_cls_mast.emp_cls_mast_id = student_detail.emp_cls_mast_id inner join student_mast on student_detail.st_id = student_mast.st_id inner join cls_register_detail on cls_register_detail.cls_reg_detail_id = emp_cls_mast.cls_reg_detail_id inner join cls_register_mast on cls_register_mast.cls_reg_id  = cls_register_detail.cls_reg_id inner join emp_mast on emp_cls_mast.emp_id  =  emp_mast.emp_id where  student_mast.family_code = '" + familycode.Text + "' and student_mast.register_date = '" + Convert.ToDateTime(TextBox42.Text).AddYears(-543).ToString("yyyy-MM-dd") + "' ";
                MySqlDataAdapter da2 = new MySqlDataAdapter(strSQL2, Conn2);
                DataSet ds2 = new DataSet();
                da2.Fill(ds2, "student_detail");
                GridView4.DataSource = ds2.Tables["student_detail"];
                GridView4.DataBind();
            }

          
        }

        void searchclsname()
        {
            MySqlConnection Conn = new MySqlConnection(StrConn);
            string strSQL;
            //strSQL = "SELECT cls_reg_code,cls_name FROM cls_register_mast     WHERE cls_reg_code ='" + TextBox22.Text + "'";
            strSQL = "select * FROM  cls_register_detail    inner join cls_register_mast on cls_register_mast.cls_reg_id = cls_register_detail.cls_reg_id  WHERE cls_register_mast.cls_reg_code ='" + TextBox22.Text + "'";
            Conn.Open();
            MySqlDataAdapter da4 = new MySqlDataAdapter(strSQL, Conn);
            DataSet ds4 = new DataSet();
            da4.Fill(ds4);

            int _row = ds4.Tables[0].Rows.Count;
            int i = 0;
            if (_row > i)
            {


                TextBox23.Text = (string)ds4.Tables[0].Rows[i]["cls_name"].ToString();
                TextBox38.Text = (string)ds4.Tables[0].Rows[i]["cls_reg_detail_id"].ToString();


            }
            else
            {
                TextBox23.Text = "ไม่มีข้อมูล";

            }
        }

        protected void Button8_Click(object sender, EventArgs e)
        {


            searchclsname();

            MySqlConnection Conn2 = new MySqlConnection(StrConn);

            string _query = "select * from cls_register_mast inner join cls_register_detail on cls_register_mast.cls_reg_id = cls_register_detail.cls_reg_id inner join emp_cls_mast on emp_cls_mast.cls_reg_detail_id = cls_register_detail.cls_reg_detail_id  inner join emp_mast on emp_cls_mast.emp_id = emp_mast.emp_id  WHERE cls_register_mast.cls_reg_code ='" + TextBox22.Text + "'";
            Conn2.Open();
            MySqlDataAdapter da2 = new MySqlDataAdapter(_query, Conn2);
            DataSet ds2 = new DataSet();
            da2.Fill(ds2);
            da2.Fill(ds2, "cls_register_mast");
            GridView3.DataSource = ds2.Tables["cls_register_mast"];
            GridView3.DataBind();
          


        }



        protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

      

        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // loop all data rows
                foreach (DataControlFieldCell cell in e.Row.Cells)
                {
                    // check all cells in one row
                    foreach (Control control in cell.Controls)
                    {
                        // Must use LinkButton here instead of ImageButton
                        // if you are having Links (not images) as the command button.
                        ImageButton button = control as ImageButton;
                        if (button != null && button.CommandName == "Delete")
                            // Add delete confirmation
                            button.OnClientClick = "if (!confirm('คุณต้องการที่จะลบข้อมูล?')) return;";
                    }
                }
            }
        }



        protected void Button4_Click(object sender, EventArgs e)
        {
            cleargrid2();
        }

        
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            MySqlConnection Conn = new MySqlConnection(StrConn);
            string strSQL;
            strSQL = "DELETE FROM student_mast WHERE st_id ='" + GridView1.DataKeys[e.RowIndex].Value + "'";
            Conn.Open();
            objCmd = new MySqlCommand(strSQL, Conn);

            objCmd.ExecuteNonQuery();



            GridView1.EditIndex = -1;

            Datastudent();
            Searchstidgridview1();
            clear();
            Button1.Enabled = true;
            Button5.Enabled = true;
        }

        protected void Button10_Click(object sender, EventArgs e)
        {

            if (st_codesend.Text != "")
            {
                Session["family_code"] = facodesend.Text;
                Session["register_date"] = regissend.Text;
                Session["family_name"] = fanamesend.Text;
                Session["st_code"] = st_codesend.Text;
                Session["passrun"] = "passrun";
                Response.Redirect("~/Pages/FamilySearchData.aspx");
            }
            else
            {
                Response.Redirect("~/Pages/FamilySearch.aspx");
            }
        }


        protected void rdobutton_CheckedChanged(object sender, EventArgs e)
        {

            RadioButton chkDel;

            int i;
            for (i = 0; i <= GridView3.Rows.Count - 1; i++)
            {
                chkDel = (RadioButton)GridView3.Rows[i].FindControl("rdobutton");

                if (chkDel.Checked)
                {

                    TextBox45.Text = ((Label)GridView3.Rows[i].FindControl("Label28")).Text.Trim();
                    TextBox46.Text = ((Label)GridView3.Rows[i].FindControl("Label27")).Text.Trim();


                }

            }

        }

        protected void GridView4_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            MySqlConnection Conn = new MySqlConnection(StrConn);
            string strSQL;
            strSQL = "DELETE FROM student_detail WHERE st_detail_id ='" + GridView4.DataKeys[e.RowIndex].Value + "'";
            Conn.Open();
            objCmd = new MySqlCommand(strSQL, Conn);

            objCmd.ExecuteNonQuery();



            GridView4.EditIndex = -1;
            Bindstid();
            Button3.Enabled = true;
        }

        protected void GridView4_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                // loop all data rows
                foreach (DataControlFieldCell cell in e.Row.Cells)
                {
                    // check all cells in one row
                    foreach (Control control in cell.Controls)
                    {
                        // Must use LinkButton here instead of ImageButton
                        // if you are having Links (not images) as the command button.
                        ImageButton button = control as ImageButton;
                        if (button != null && button.CommandName == "Delete")
                            // Add delete confirmation
                            button.OnClientClick = "if (!confirm('คุณต้องการที่จะลบข้อมูล?')) return;";
                    }
                }
            }

        }
        void discountdata()
        {
            MySqlConnection Conn2 = new MySqlConnection(StrConn);

            string _query = "select * from discount";
            Conn2.Open();
            MySqlDataAdapter da2 = new MySqlDataAdapter(_query, Conn2);
            DataSet ds2 = new DataSet();
            da2.Fill(ds2);
            this.DropDownList1.DataSource = ds2;
            this.DropDownList1.DataTextField = "Name_Discount";
            this.DropDownList1.DataValueField = "id_Discount";
            this.DropDownList1.DataBind();
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Label39.Text = DropDownList1.SelectedValue;
            MySqlConnection Conn2 = new MySqlConnection(StrConn);

            string _query = "select * from discount where id_Discount = '" + Label39.Text + "' ";
            Conn2.Open();
            MySqlDataAdapter da2 = new MySqlDataAdapter(_query, Conn2);
            DataSet ds2 = new DataSet();
            da2.Fill(ds2);

            int _row2 = ds2.Tables[0].Rows.Count;
            int i2 = 0;
            if (_row2 > i2)
            {


                Label40.Text = (string)ds2.Tables[0].Rows[i2]["Num_Discount"].ToString();




            }
        }
    }
}